package com.itheima.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import pojo.OrderSetting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预约设置服务
 */
@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {
    
    @Autowired
    private OrderSettingDao orderSettingDao;
    
    @Override
    public void add(List<OrderSetting> orderSettings) {
        if(orderSettings != null && orderSettings.size() > 0){
            for(OrderSetting orderSetting : orderSettings){
                //检查此日期是否存在
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(count > 0){
                    //已经存在，进行更新
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else{
                    //进行添加
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }
    
    //根据日期查询预约设置
    @Override
    public List<Map> getOrderSettingByMonthgetOrderSettingByMonth(String date) {
        //date: 2022-09
        //设置日期开始时间
        String dateBegin = date + "-1";
        //设置日期结束时间
        String dateEnd = date + "-31";
        //封装为Map
        Map map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        
        //查找此时间段里(此月份里)的预约设置
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        
        //转为Map集合
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map m = new HashMap();
            m.put("date",orderSetting.getOrderDate().getDate());//获得几号
            m.put("number",orderSetting.getNumber());//可预约人数
            m.put("reservations",orderSetting.getReservations());//已预约人数
            data.add(m);
        }
        return data;
    }
    
    //进行预约设置(添加预约设置或修改已存在预约设置)
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(count > 0){
            //代表这个日期已经存在预约设置了，
            //只需修改可预约数量
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //这个日期还没有预约设置，添预约设置
            orderSettingDao.add(orderSetting);
        }
    }
}
